﻿package mortal.game.view.copy.sunbathCopy
{
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.ai.data.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;

    public class SunbathUtil extends Object
    {
        public static var doingAction:String = "";
        private static var kissSkillId:int = 51300002;
        private static var shootSkillId:int = 51300001;
        private static var type:int;
        private static var cost:int;
        private static var sEntityId:SEntityId;
        private static var _perPlayer:MovePlayer;
        public static var isInSunbatch:Boolean = false;
        public static var isInPerFormNow:Boolean = false;
        private static var _backFun:Function;
        private static var _paramArr:Array;

        public function SunbathUtil()
        {
            return;
        }// end function

        public static function getSkillInfoArr() : Array
        {
            var _loc_2:Object = null;
            var _loc_1:Array = [];
            var _loc_3:* = Cache.instance.copy.sunbath.subPlayerInfo;
            _loc_2 = {};
            _loc_2["nameBg"] = ImagesConst.SunbathCopyTxt5;
            _loc_2["num0"] = _loc_3 ? (_loc_3.kissCount) : (0);
            _loc_2["num1"] = 5 - _loc_2["num0"];
            _loc_2["type"] = ESunbathOper._ESunbathOperKiss;
            _loc_1.push(_loc_2);
            _loc_2 = {};
            _loc_2["nameBg"] = ImagesConst.SunbathCopyTxt3;
            _loc_2["num0"] = _loc_3 ? (_loc_3.shootCount) : (0);
            _loc_2["num1"] = 5 - _loc_2["num0"];
            _loc_2["type"] = ESunbathOper._ESunbathOperShoot;
            _loc_1.push(_loc_2);
            _loc_2 = {};
            _loc_2["nameBg"] = ImagesConst.SunbathCopyTxt6;
            _loc_2["num0"] = _loc_3 ? (_loc_3.performCount) : (0);
            _loc_2["num1"] = _loc_3 ? (_loc_3.performPiece) : (5);
            _loc_2["type"] = ESunbathOper._ESunbathOperPerform;
            _loc_1.push(_loc_2);
            _loc_2 = {};
            _loc_2["nameBg"] = ImagesConst.SunbathCopyTxt7;
            _loc_2["num0"] = 0;
            _loc_2["num1"] = 5;
            _loc_2["type"] = ESunbathOper._ESunbathOperDance;
            _loc_2["active"] = 1;
            _loc_1.push(_loc_2);
            return _loc_1;
        }// end function

        public static function oper(param1:Object) : void
        {
            if (param1 == null)
            {
                return;
            }
            type = 0;
            sEntityId = null;
            switch(param1["type"])
            {
                case ESunbathOper._ESunbathOperKiss:
                {
                    if (isInPreform())
                    {
                        SunbathUtil.showQuickPerformAlert(kissHd, null, "" + Language.getString(70983));
                    }
                    else
                    {
                        kissHd();
                    }
                    break;
                }
                case ESunbathOper._ESunbathOperShoot:
                {
                    if (isInPreform())
                    {
                        SunbathUtil.showQuickPerformAlert(shootHd, null, "" + Language.getString(70984));
                    }
                    else
                    {
                        shootHd();
                    }
                    break;
                }
                case ESunbathOper._ESunbathOperPerform:
                {
                    performHd();
                    break;
                }
                case ESunbathOper._ESunbathOperDance:
                {
                    if (param1["active"])
                    {
                        activeDanceHd();
                    }
                    else
                    {
                        danceHd(param1);
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private static function canPerform() : Boolean
        {
            if (Cache.instance.copy.sunbath.subPlayerInfo.performPiece > 0 || Cache.instance.copy.sunbath.subPlayerInfo.performTime > 0)
            {
                return true;
            }
            return false;
        }// end function

        private static function performHd() : void
        {
            type = ESunbathOper._ESunbathOperPerform;
            if (!canPerform())
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70985));
                return;
            }
            AIManager.cancelAll();
            reqOpe();
            return;
        }// end function

        private static function onCloseHandler(param1:int) : void
        {
            if (param1 == Alert.YES && type > 0)
            {
                if (type == ESunbathOper._ESunbathOperPerform && Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGold, cost) == false)
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(70974));
                    return;
                }
                reqOpe();
            }
            return;
        }// end function

        private static function reqOpe() : void
        {
            if (type != 0)
            {
                if (sEntityId == null)
                {
                    sEntityId = new SEntityId();
                }
                GameProxy.copy.sunbathOper(type, sEntityId);
            }
            return;
        }// end function

        private static function activeDanceHd() : void
        {
            var _loc_1:Boolean = false;
            var _loc_5:SpritePlayer = null;
            type = ESunbathOper._ESunbathOperDance;
            _perPlayer = null;
            var _loc_2:* = RolePlayer.instance.x2d;
            var _loc_3:* = RolePlayer.instance.y2d;
            if (RolePlayer.instance.entityInfo.isInPlayMusical)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70986));
                return;
            }
            var _loc_4:* = RolePlayer.instance.entityInfo.entityInfo.sex;
            for each (_loc_5 in ThingUtil.entityUtil.entitysMap.allEntitys)
            {
                
                if (_loc_5 is UserPlayer && _loc_5 is RolePlayer == false)
                {
                    if (_loc_5.entityInfo.isInPlayMusical && _loc_5.entityInfo.entityInfo.sex != _loc_4)
                    {
                        _loc_1 = true;
                        sEntityId = _loc_5.entityInfo.entityInfo.entityId;
                        _perPlayer = _loc_5 as MovePlayer;
                        break;
                    }
                }
            }
            if (!_loc_1)
            {
                MsgManager.showRollTipsMsg(Language.getString(70987));
                return;
            }
            goAndDance();
            return;
        }// end function

        private static function danceHd(param1:Object) : void
        {
            var _loc_2:Boolean = false;
            var _loc_6:SpritePlayer = null;
            type = ESunbathOper._ESunbathOperDance;
            sEntityId = param1["sentity"];
            if (sEntityId == null)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70988));
                return;
            }
            var _loc_3:* = RolePlayer.instance.x2d;
            var _loc_4:* = RolePlayer.instance.y2d;
            var _loc_5:* = RolePlayer.instance.entityInfo.entityInfo.sex;
            for each (_loc_6 in ThingUtil.entityUtil.entitysMap.allEntitys)
            {
                
                if (_loc_6 is UserPlayer && _loc_6 is RolePlayer == false && EntityUtil.equal(_loc_6.entityInfo.entityInfo.entityId, sEntityId))
                {
                    _perPlayer = _loc_6 as MovePlayer;
                    _loc_2 = true;
                    break;
                }
            }
            if (!_loc_2)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70989));
                return;
            }
            goAndDance();
            return;
        }// end function

        private static function kissHd() : void
        {
            type = ESunbathOper._ESunbathOperKiss;
            useSkill("" + Language.getString(70990));
            return;
        }// end function

        private static function shootHd() : void
        {
            type = ESunbathOper._ESunbathOperShoot;
            useSkill("" + Language.getString(70991));
            return;
        }// end function

        private static function useSkill(param1:String) : void
        {
            var _loc_2:* = ThingUtil.selectEntity;
            if (_loc_2 == null || !(_loc_2 is UserPlayer))
            {
                MsgManager.showRollTipsMsg(param1);
                return;
            }
            AIManager.cancelAll();
            var _loc_3:* = new FollowAI();
            var _loc_4:* = AIFactory.instance.outAIData();
            _loc_3.data = _loc_4;
            _loc_4.target = _loc_2;
            var _loc_5:* = getAtionDis(type);
            _loc_4.range = getAtionDis(type);
            _loc_3.range = _loc_5;
            _loc_4.meRole = RolePlayer.instance;
            _loc_4.scene = Game.scene;
            AIManager.addAI(_loc_3);
            AIManager.addCallback(moveEnd, null);
            AIManager.start();
            return;
        }// end function

        private static function moveEnd() : void
        {
            var _loc_1:* = ThingUtil.selectEntity;
            if (_loc_1 == null)
            {
                return;
            }
            if (isInRange(_loc_1 as MovePlayer))
            {
                sEntityId = _loc_1.entityInfo.entityInfo.entityId;
                reqOpe();
            }
            return;
        }// end function

        private static function goAndDance() : void
        {
            if (_perPlayer == null)
            {
                return;
            }
            AIManager.cancelAll();
            var _loc_1:* = new FollowAI();
            var _loc_2:* = AIFactory.instance.outAIData();
            _loc_1.data = _loc_2;
            _loc_2.target = _perPlayer;
            var _loc_3:* = getAtionDis(type);
            _loc_2.range = getAtionDis(type);
            _loc_1.range = _loc_3;
            _loc_2.meRole = RolePlayer.instance;
            _loc_2.scene = Game.scene;
            AIManager.addAI(_loc_1);
            AIManager.addCallback(toDance, null);
            AIManager.start();
            return;
        }// end function

        private static function toDance() : void
        {
            if (_perPlayer == null)
            {
                return;
            }
            if (ESunbathOper._ESunbathOperDance != type)
            {
                return;
            }
            if (GeomUtil.calcDistance(_perPlayer.x2d, _perPlayer.y2d, RolePlayer.instance.x2d, RolePlayer.instance.y2d) <= getAtionDis(type))
            {
                sEntityId = _perPlayer.entityInfo.entityInfo.entityId;
                reqOpe();
            }
            _perPlayer = null;
            return;
        }// end function

        private static function isInRange(param1:MovePlayer) : Boolean
        {
            var _loc_4:RolePlayer = null;
            var _loc_2:int = 0;
            try
            {
                _loc_4 = RolePlayer.instance;
                _loc_2 = GameMapUtil.getDistance(_loc_4.x2d, _loc_4.y2d, param1.x2d, param1.y2d);
            }
            catch (e)
            {
            }
            var _loc_3:* = _loc_2 <= getAtionDis(type);
            return _loc_3;
        }// end function

        public static function getPassWinReward() : Array
        {
            var _loc_1:Array = [];
            _loc_1.push(Cache.instance.copy.sunbath.exp);
            _loc_1.push(Cache.instance.copy.sunbath.vitalEnergy);
            return _loc_1;
        }// end function

        public static function getDanceList() : Array
        {
            var _loc_3:* = undefined;
            var _loc_1:Array = [];
            var _loc_2:* = Cache.instance.copy.sunbath.danceInfo.performers;
            for (_loc_3 in _loc_2)
            {
                
                _loc_1.push({nameA:_loc_2[_loc_3], info:_loc_3});
            }
            if (_loc_1.length > 1)
            {
                _loc_1.sortOn("nameA");
            }
            return _loc_1;
        }// end function

        private static function getAtionDis(param1:int) : int
        {
            var _loc_2:int = 100;
            switch(param1)
            {
                case ESunbathOper._ESunbathOperDance:
                {
                    _loc_2 = GameConst.TConstSunBathDanceRange;
                    break;
                }
                case ESunbathOper._ESunbathOperKiss:
                {
                    _loc_2 = SkillConfig.instance.getInfoById(kissSkillId).distance;
                    break;
                }
                case ESunbathOper._ESunbathOperShoot:
                {
                    _loc_2 = SkillConfig.instance.getInfoById(shootSkillId).distance;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_2;
        }// end function

        public static function isInPreform() : Boolean
        {
            if (isInSunbatch == false)
            {
                return false;
            }
            return isInPerFormNow;
        }// end function

        public static function showQuickPerformAlert(param1:Function, param2:Array, param3:String = null) : void
        {
            _backFun = param1;
            _paramArr = param2;
            var _loc_4:* = param3;
            if (_loc_4 == null)
            {
                _loc_4 = "" + Language.getString(70992);
            }
            Alert.show(_loc_4, null, Alert.YES | Alert.NO, null, onMove);
            return;
        }// end function

        private static function onMove(param1:int) : void
        {
            if (param1 == Alert.YES)
            {
                if (_backFun != null)
                {
                    if (_paramArr != null)
                    {
                        _backFun.apply(null, _paramArr);
                    }
                    else
                    {
                        _backFun();
                    }
                }
            }
            return;
        }// end function

    }
}
